package atm05;

import atm04.FileUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 存款界面
 */
public class SavePanel extends JPanel implements ActionListener{
    private ATM05 atm05;
    private JLabel input = new JLabel("请输入您的存款金额：");
    private JTextField inputText = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("src/atm05/Images/buttonSure.jpg"));
    private JButton exitBtn = new JButton(new ImageIcon("src/atm05/Images/buttonOff.jpg"));
    private Font font = new Font("黑体",Font.BOLD,25);

    public SavePanel(ATM05 atm05) {
        this.atm05 = atm05;
        this.setLayout(null);

        input.setBounds(280,250,300,50);
        input.setFont(font);

        inputText.setBounds(550,250,200,50);
        inputText.setFont(font);

        sureBtn.setBounds(420,320,140,50);
        sureBtn.addActionListener(this);

        exitBtn.setBounds(880,380,140,50);
        exitBtn.addActionListener(this);

        this.add(input);
        this.add(inputText);
        this.add(sureBtn);
        this.add(exitBtn);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if (e.getSource() == sureBtn){
                double blance = Double.parseDouble(inputText.getText());
                if (blance <= 0){
                    JOptionPane.showMessageDialog(this,"请输入正确的金额");
                    return;
                }
                int index = atm05.getIndex();
                atm05.getUsers().get(index).setBlance(atm05.getUsers().get(index).getBlance() + blance);
                FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
                JOptionPane.showMessageDialog(this,"存款成功");
                inputText.setText("");
                atm05.showPanel("menu");
            }else if (e.getSource() == exitBtn){
                atm05.showPanel("menu");
            }
        }catch (Exception e1){
            JOptionPane.showMessageDialog(this,"请输入正确的金额");
            inputText.setText("");
        }
    }
}
